﻿using ErrorOr;
using Gym.Domain.AdminAggregate.Events;
using Gym.Domain.Common;
using Gym.Domain.SubscriptionAggregate;

namespace Gym.Domain.AdminAggregate
{
    public class Admin : AggregateRoot
    {
        public Guid UserId { get; }
        public Guid? SubscriptionId { get; private set; }

        public Admin(
            Guid userId,
            Guid? subscriptionId = null,
            Guid? id = null) : base(id ?? Guid.NewGuid())
        {            
            UserId = userId;
            SubscriptionId = subscriptionId;
        }

        private Admin()
        {
        }

        public ErrorOr<Success> SetSubscription(Subscription subscription)
        {
            if (SubscriptionId.HasValue) 
            {
                return Error.Conflict("管理员已有一个有效订阅");
            }

            SubscriptionId = subscription.Id;

            _domainEvents.Add(new SubscriptionSetEvent(this, subscription));

            return Result.Success;
        }
    }
}
